﻿///<reference path="../../scripts/jquery-1.4.4.min.js" />
///<reference path="../../scripts/jquery-ui.min.js" />
///<reference path="../../scripts/kooboo/kooboo.form.js" />

$(function () {

    function render() {
        var isEven = false;
        $("#tax-container").empty();
        for (var i = 0; i < taxes.length; i++) {
            var item = taxes[i];
            var row = $("#tax-row").find(".template").clone();

            row.find(".edit").attr("href", i);
            row.find("input[name=Id]").val(item.Id);
            row.find("td:eq(0)").html(item.Name);
            row.find("td:eq(1)").html(item.Percent + "%");
            row.find("td:eq(2)").html(item.SubTaxes.length);

            if (isEven) {
                row.addClass("even");
            }
            isEven = !isEven;

            $("#tax-container").append(row);

        }
    }

    render();


    //events

    //1.add
    $(".tax-add").click(function (e) {
        e.preventDefault();

        $("#tax-form").attr("action", add_action);

        $("input[name=Id]", "#tax-form").val("-1");
        $("input[name=Name]", "#tax-form").val("");
        $("input[name=Percent]", "#tax-form").val("0");
        $("#sub-tax-container").empty();

        $("#tax-dialog").dialog("open");
        $("input:first", "#tax-form").focus();

        return false;
    });


    //2.save
    $("#tax-form").submit(function () {
        $(this).ajaxPost({
            success: function (item) {
                $("#tax-dialog").dialog("close");

                var hasFound = false;
                for (var i = 0; i < taxes.length; i++) {
                    if (taxes[i].Id == item.Id) {
                        taxes[i] = item;
                        hasFound = true;
                        break;
                    }
                }

                if (hasFound == false) {
                    taxes.splice(0, 0, item);
                }

                render();
            },
            box: $("#tax-dialog")
        });
        return false;
    });

    var index = 0;
    //2.1 add rule
    $('.subtax-add').click(function () {
        var row = $("#sub-tax-row").find(".template").clone();
        index++;

        var prefix = "SubTaxes[" + index + "]";

        row.find(".country").attr("name", prefix + ".Country.Code").find("option:first").attr("selected", true);
        row.find(".state").val("").attr("name", prefix + ".State");
        row.find(".percent").val("0").attr("name", prefix + ".Percent");
        row.find(".id").val("-1").attr("name", prefix + ".Id");
        row.find(".index").val(index);


        $("#sub-tax-container").append(row);
    });

    //2.2 Remove rule
    $('.o-icon.remove', "#sub-tax-container").live("click", function (e) {
        e.preventDefault();
        if (confirm($(this).attr("message"))) {
            $(this).parents('tr').remove();
        }
        return false;
    });

    //3 edit
    $(".edit", "#tax-container").live("click", function (e) {
        e.preventDefault();

        var item = taxes[parseInt($(this).attr("href"))];

        $("#tax-form").attr("action", edit_action);

        $("input[name=Id]", "#tax-form").val(item.Id);
        $("input[name=Name]", "#tax-form").val(item.Name);
        $("input[name=Percent]", "#tax-form").val(item.Percent);
        $("#sub-tax-container").empty();

        index = 0;
        for (var i = 0; i < item.SubTaxes.length; i++) {
            var subtax = item.SubTaxes[i];

            var row = $("#sub-tax-row").find(".template").clone();
            index++;

            var prefix = "SubTaxes[" + index + "]";

            row.find(".country").attr("name", prefix + ".Country.Code").val(subtax.CountryCode);
            row.find(".state").val(subtax.State).attr("name", prefix + ".State");
            row.find(".percent").val(subtax.Percent).attr("name", prefix + ".Percent");
            row.find(".id").val(subtax.Id).attr("name", prefix + ".Id");
            row.find(".index").val(index);


            $("#sub-tax-container").append(row);
        }


        $("#tax-dialog").dialog("open");
        $("input:first", "#tax-form").focus();

        return false;
    })

    //4 delete
    $(".delete-link", "#tax-container").live("click", function (e) {
        e.preventDefault();
        if (confirm($(this).attr("message"))) {
            $(this).parents("form:first").submit();
        }
    });

    $("form", "#tax-container").live("submit", function (e) {
        var form = $(this);
        $(this).ajaxPost({
            success: function () {
                row = form.parents("tr:first");
                var index = row.prevAll().size();

                row.remove();    
                taxes.splice(index, 1);
            }
        });

        return false;
    })
})